######################################################################
#Replication file for "The Impact of China's AIIB on the World Bank"
#Authors: Jing Qian, James Vreeland, Jianzhi Zhao
#Codes to replicate results in Table C.6
######################################################################
rm(list = ls())

#Other packages used
library(tidyverse)
library(lmtest)
library(sandwich)
library(MASS)
library(stargazer)

#Setwd
setwd("C:/Users/qianj/Dropbox (Princeton)/AIIB_WB_Replication")

#Load custom functions
#Note: Given the number of tests performed in the appendix, 
#several custom functions are created to implement estimation methods in a more efficient way.
#These functions merely create a wrap-up of existing functions, like bpCausal and gsynth,
#in order to avoid typing parameters repeatedly.

source("code/custom_functions.R")

###################################
#Produce Table C.5
###################################
#-----------------------
#(1) Load data
#-----------------------
load("data/data_condition_rep.RData")

#----------------------------
#(2) Column (1)
#----------------------------
fit.tab.c6.1 = glm.nb(count_pa ~ aiib_founder_2016 + absidealimportantdiff + board + colony + unsc + USaid + CHaid + gdppc + 
                        dservtoGDP + dshorttoexports + inflation + debttoGDP + FDItoGDP + polity2 + 
                        openness + war + elec + IMF + crisis + factor(ccode), 
                      data = df.condition.replicate, 
                      control = glm.control(maxit=20))
se.tab.c6.1 = coeftest(fit.tab.c6.1, vcov = vcovHC(fit.tab.c6.1, type = "HC0", cluster = df.condition.replicate$ccode))

#----------------------------
#(3) Column (2)
#----------------------------
fit.tab.c6.2 = glm.nb(numcats ~ aiib_founder_2016 + absidealimportantdiff + board + colony + unsc + USaid + CHaid + gdppc + 
                        dservtoGDP + dshorttoexports + inflation + debttoGDP + FDItoGDP + polity2 + 
                        openness + war + elec + IMF + crisis + factor(ccode), 
                      data = df.condition.replicate, 
                      control=glm.control(maxit=20))

se.tab.c6.2 = coeftest(fit.tab.c6.2, vcov = vcovHC(fit.tab.c6.2, type = "HC0", cluster = wb.analysis.condition.rep$ccode))

#----------------------------
#(4) Combine
#----------------------------
stargazer(list(fit.tab.c6.1,
               fit.tab.c6.2),
          se = list(se.tab.c6.1[,2],
                    se.tab.c6.2[,2]),
          omit = c("ccode"),
          type = "text")
